extends Node2D

# 战斗场景脚本

# 战斗状态
enum BattleState {PLAYER_TURN, ENEMY_TURN, WIN, LOSE}
var current_state = BattleState.PLAYER_TURN

# 战斗数据
var player_hp = 100
var player_max_hp = 100
var player_attack = 15
var player_defense = 10

var enemy_hp = 80
var enemy_max_hp = 80
var enemy_attack = 12
var enemy_defense = 8

# 初始化战斗场景
func _ready():
	print("战斗场景初始化")
	update_battle_ui()
	$BattleLog.text = "战斗开始！轮到你的回合。"

# 更新战斗UI
func update_battle_ui():
	$PlayerContainer/PlayerHP.text = "HP: " + str(player_hp) + "/" + str(player_max_hp)
	$EnemyContainer/EnemyHP.text = "HP: " + str(enemy_hp) + "/" + str(enemy_max_hp)
	
	match current_state:
		BattleState.PLAYER_TURN:
			$ActionContainer/StatusLabel.text = "你的回合"
			$ActionContainer/AttackButton.disabled = false
			$ActionContainer/SkillButton.disabled = false
			$ActionContainer/ItemButton.disabled = false
		BattleState.ENEMY_TURN:
			$ActionContainer/StatusLabel.text = "敌方回合"
			$ActionContainer/AttackButton.disabled = true
			$ActionContainer/SkillButton.disabled = true
			$ActionContainer/ItemButton.disabled = true
		BattleState.WIN:
			$ActionContainer/StatusLabel.text = "战斗胜利！"
			$ActionContainer/AttackButton.disabled = true
			$ActionContainer/SkillButton.disabled = true
			$ActionContainer/ItemButton.disabled = true
		BattleState.LOSE:
			$ActionContainer/StatusLabel.text = "战斗失败！"
			$ActionContainer/AttackButton.disabled = true
			$ActionContainer/SkillButton.disabled = true
			$ActionContainer/ItemButton.disabled = true

# 攻击按钮回调
func _on_AttackButton_pressed():
	if current_state != BattleState.PLAYER_TURN:
		return
	
	# 计算伤害
	var damage = max(1, player_attack - enemy_defense / 2)
	enemy_hp = max(0, enemy_hp - damage)
	
	# 更新UI
	$BattleLog.text = "你的幻兽攻击敌方幻兽，造成 " + str(damage) + " 点伤害！"
	update_battle_ui()
	
	# 检查战斗结果
	if enemy_hp <= 0:
		current_state = BattleState.WIN
		$BattleLog.text = "你的幻兽击败了敌方幻兽！战斗胜利！"
		update_battle_ui()
		return
	
	# 切换到敌方回合
	current_state = BattleState.ENEMY_TURN
	update_battle_ui()
	
	# 延迟执行敌方行动
	await get_tree().create_timer(1.0).timeout
	enemy_action()

# 技能按钮回调
func _on_SkillButton_pressed():
	if current_state != BattleState.PLAYER_TURN:
		return
	
	# 计算伤害（技能伤害略高）
	var damage = max(1, player_attack * 1.5 - enemy_defense / 2)
	enemy_hp = max(0, enemy_hp - damage)
	
	# 更新UI
	$BattleLog.text = "你的幻兽使用技能攻击敌方幻兽，造成 " + str(damage) + " 点伤害！"
	update_battle_ui()
	
	# 检查战斗结果
	if enemy_hp <= 0:
		current_state = BattleState.WIN
		$BattleLog.text = "你的幻兽击败了敌方幻兽！战斗胜利！"
		update_battle_ui()
		return
	
	# 切换到敌方回合
	current_state = BattleState.ENEMY_TURN
	update_battle_ui()
	
	# 延迟执行敌方行动
	await get_tree().create_timer(1.0).timeout
	enemy_action()

# 道具按钮回调
func _on_ItemButton_pressed():
	if current_state != BattleState.PLAYER_TURN:
		return
	
	# 恢复生命值
	var heal_amount = 20
	player_hp = min(player_max_hp, player_hp + heal_amount)
	
	# 更新UI
	$BattleLog.text = "你使用了恢复药剂，恢复了 " + str(heal_amount) + " 点生命值！"
	update_battle_ui()
	
	# 切换到敌方回合
	current_state = BattleState.ENEMY_TURN
	update_battle_ui()
	
	# 延迟执行敌方行动
	await get_tree().create_timer(1.0).timeout
	enemy_action()

# 敌方行动
func enemy_action():
	if current_state != BattleState.ENEMY_TURN:
		return
	
	# 计算伤害
	var damage = max(1, enemy_attack - player_defense / 2)
	player_hp = max(0, player_hp - damage)
	
	# 更新UI
	$BattleLog.text = "敌方幻兽攻击你的幻兽，造成 " + str(damage) + " 点伤害！"
	update_battle_ui()
	
	# 检查战斗结果
	if player_hp <= 0:
		current_state = BattleState.LOSE
		$BattleLog.text = "你的幻兽被击败了！战斗失败！"
		update_battle_ui()
		return
	
	# 切换到玩家回合
	current_state = BattleState.PLAYER_TURN
	update_battle_ui()
	$BattleLog.text = "轮到你的回合。"

# 返回按钮回调
func _on_BackButton_pressed():
	print("返回世界地图")
	get_tree().change_scene_to_file("res://scenes/world.tscn")
